class DocumentsController < ApplicationController
  before_filter :find_models, :only => :index
	before_filter :find_category_instance, :only => [:new, :create]
	before_filter :find_model_instance, :only => [:show, :edit, :update, :destroy] 
  # GET /documents
  # GET /documents.xml
  def index
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @documents }
    end
  end

  # GET /documents/1
  # GET /documents/1.xml
  def show
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @document }
    end
  end

  # GET /documents/new
  # GET /documents/new.xml
  def new
    @document = Document.new
    @document.document_category_id = params[:document_category_id]

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @document }
    end
  end

  # GET /documents/1/edit
  def edit
  end

  # POST /documents
  # POST /documents.xml
  def create
    @document = Document.new(params[:document])
    @document.document_category = @document_category
    respond_to do |format|
      if @document.save
        flash[:notice] = 'Document was successfully created.'
        format.html { redirect_to(:action => :index,
            :document_category_id => params[:document_category_id],
            :document_id => @document) }
        format.xml  { render :xml => @document, :status => :created, :location => @document }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @document.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /documents/1
  # PUT /documents/1.xml
  def update
    respond_to do |format|
      if @document.update_attributes(params[:document])
        flash[:notice] = 'Document was successfully updated.'
        format.html { redirect_to(:action => :index,
            :document_category_id => params[:document_category_id],
            :document_id => @document) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @document.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /documents/1
  # DELETE /documents/1.xml
  def destroy
    @document.destroy
    respond_to do |format|
      format.html { redirect_to(documents_url) }
      format.xml  { head :ok }
    end
  end
  
  def destroy_version
    find_document_version
    @document_version.destroy
    respond_to do |format|
      format.html { redirect_to(:action => :edit,
          :document_category_id => params[:document_category_id],
          :document_id => @document) }
      format.xml  { head :ok }
    end
  end

  def download
    doc = Document.find(params[:id])
    unless content_allowed?(doc.content, current_user_user_group_ids)
      raise SecurityError, "Access to resource not allowed"
    end
    send_file(doc.current_version.version.path)
  end

  private

  def find_category_instance
    @document_category = DocumentCategory.find(params[:document_category_id]) 
  rescue ActiveRecord::RecordNotFound
    raise(SecurityError, "Document not found matching document category.") 
  end

  def find_models
    unless params[:document_category_id]
      flash[:notice] = "Documents page requires Document Category association"
      redirect_back_or_default("/")
      return
    end 
    find_category_instance
    @documents = Document.find(:all, 
      :conditions => ["documents.document_category_id = ?", @document_category.id])
  end

  def find_model_instance
    find_category_instance
    @document = Document.find(params[:id], 
      :conditions => ["documents.document_category_id = ?", @document_category.id])
    unless content_allowed?(@document.content, current_user_user_group_ids)
      raise SecurityError, "Access to resource not allowed"
    end
  rescue ActiveRecord::RecordNotFound
    raise(SecurityError, "Document not found matching document category.") 
  end

  def find_document_version
    find_category_instance
    find_model_instance
    if @document.document_versions.length == 1
      flash[:error] = "Cannot remove last version."
      return
    end
    @document_version = DocumentVersion.find(params[:document_version_id])
  rescue ActiveRecord::RecordNotFound
    raise(SecurityError, "Document Version not found.") 
  end

end
